import { defineStore } from 'pinia'
// import { getTopData} from '@/service/main/main'
import {
  getTopData, getFrontData, getBottomData, getlineChart
} from '@/service/main/analysis/analysis'
interface IMainState {
  topDate: any[],
  frontData: any,
  frontlist: any,
  bottomData: any[],
  bottomList: any[],
  chartDate: any[],
}

const useMainStore = defineStore('main', {
  state: (): IMainState => ({
    topDate: [],
    frontData: {},
    frontlist: [],
    bottomData: [],
    bottomList: [],
    chartDate: [],
  }),
  actions: {
    async fetchEntireDateActive() {
      const roles = await getTopData()
      this.topDate = roles.data?.list[0]
      return this.topDate
    },
    async getFrontDataActive() {
      // const frontData = await getFrontData()
      // this.frontData = frontData.data?.list[0]
      this.frontlist = [{
        title: '测温范围（℃）',
        num: '1: 800...2700',
      }, {
        title: '图像频率 (Hz)',
        num: '50',
      }, {
        title: '加权值',
        num: '1.0',
      }, {
        title: '探测器温服 (℃)',
        num: 0,
      }, {
        title: '热像仪温度 (℃)',
        num: 0,
      }, {
        title: '环境温度 (℃)',
        num: 0,
      }]
      this.frontlist[3].num = this.frontData.detectorTemp
      this.frontlist[4].num = this.frontData.cameraTemp
      this.frontlist[5].num = this.frontData.envTemp

      return this.frontData

    },
    async getBottomData() {
      this.bottomList = [
        { name: '发射率', value: 0, m: null },
        { name: '大气透过率', value: 0, m: null },
        { name: '反射温度', value: 0, m: '℃' },
        { name: '环境温度', value: 0, m: '℃' },
        { name: '距离', value: 0, m: 'm' },
      ]
      const bottomData = await getBottomData()
      this.bottomData = bottomData.data?.list
      this.bottomList[0].value = this.bottomData[0].emissivity
      this.bottomList[1].value = this.bottomData[0].humidity
      this.bottomList[2].value = this.bottomData[0].reflectTemp
      this.bottomList[3].value = this.bottomData[0].airTemp
      this.bottomList[4].value = this.bottomData[0].distance
      this.chartDate = []
      this.bottomData.forEach((ele) => {
        this.getChartDdata(ele)
      })
      // let _this = this

      // this.bottomData = [
      //   {
      //     "emissivity": 1,
      //     "reflectTemp": 25,
      //     "distance": 2,
      //     "avTemp": 46.3,
      //     "jsonUrl": "http://192.168.1.78/file/192.168.1.31/temp.json",
      //     "IP": "192.168.1.31",
      //     "maxTemp": 85.7,
      //     "serverName": "IRCAM",
      //     "airTemp": 25,
      //     "humidity": 1,
      //     "url": "http://192.168.1.78/file/192.168.1.31/output.jpg",
      //     "minTemp": 24.5
      //   },
      //   {
      //     "emissivity": 1,
      //     "reflectTemp": 25,
      //     "distance": 2,
      //     "avTemp": 36.3,
      //     "jsonUrl": "http://192.168.1.78/file/192.168.1.29/temp.json",
      //     "IP": "192.168.1.29",
      //     "maxTemp": 81.1,
      //     "serverName": "IRCAM",
      //     "airTemp": 25,
      //     "humidity": 1,
      //     "url": "http://192.168.1.78/file/192.168.1.29/output.jpg",
      //     "minTemp": 24.7
      //   },
      //   {
      //     "emissivity": 1,
      //     "reflectTemp": 25,
      //     "distance": 2,
      //     "avTemp": 47.3,
      //     "jsonUrl": "http://192.168.1.78/file/192.168.1.30/temp.json",
      //     "IP": "192.168.1.30",
      //     "maxTemp": 78.8,
      //     "serverName": "IRCAM",
      //     "airTemp": 25,
      //     "humidity": 1,
      //     "url": "http://192.168.1.78/file/192.168.1.30/output.jpg",
      //     "minTemp": 30.8
      //   }]
      // this.bottomList[0].value = this.bottomData[0].emissivity
      // this.bottomList[1].value = this.bottomData[0].humidity
      // this.bottomList[2].value = this.bottomData[0].reflectTemp
      // this.bottomList[3].value = this.bottomData[0].airTemp
      // this.bottomList[4].value = this.bottomData[0].distance

      // this.bottomData.forEach((ele) => {
      //   this.getChartDdata(ele)
      //   // const url = new URL(ele.jsonUrl)
      //   // // const chartData = await getlineChart(url.pathname)
      //   // let obj = [{ "showSymbol": false, "data": [65, 74, 90, 70, 95, 76.4, 76.9, 73.2, 83.4, 76.1, 78.5, 75.3], "name": "最高温", "type": "line", "smooth": 0.6 }, { "showSymbol": false, "data": [48, 51, 54, 40, 65, 46.8, 46.9, 47.1, 47, 47, 47.1, 47.1], "name": "平均温", "type": "line", "smooth": 0.6 }, { "showSymbol": false, "data": [5, 34, 30, 30, 35, 31.2, 30.8, 30.4, 30.8, 30.4, 31.2, 30.8], "name": "最低温", "type": "line", "smooth": 0.6 }]
      //   // // this.chartDate.push(chartData.data?.list) 
      //   // _this.chartDate.push(obj)

      // })


      return this.bottomData
    },
    // 处理图片
    setUrl() {
      // let date = []
      this.bottomData.forEach(image => {
        var url = (image.url).split('?')[0]
        image.url = `${url}?ver=${new Date().getTime()}`;
      });
      // this.bottomData = date
    },
    oneImgUrl() {
      var url = this.frontData.url.split('?')[0]
      this.frontData.url = `${url}?ver=${new Date().getTime()}`
    },
    // 获取折线图
    async getChartDdata(date) {
      console.log(date, '-getChartDdata---');
      const url = new URL(date.jsonUrl)
      const chartData = await getlineChart(url.pathname)
      // let obj = [{ "showSymbol": false, "data": [65, 74, 90, 70, 95, 76.4, 76.9, 73.2, 83.4, 76.1, 78.5, 75.3], "name": "最高温", "type": "line", "smooth": 0.6 }, { "showSymbol": false, "data": [48, 51, 54, 40, 65, 46.8, 46.9, 47.1, 47, 47, 47.1, 47.1], "name": "平均温", "type": "line", "smooth": 0.6 }, { "showSymbol": false, "data": [5, 34, 30, 30, 35, 31.2, 30.8, 30.4, 30.8, 30.4, 31.2, 30.8], "name": "最低温", "type": "line", "smooth": 0.6 }]
      this.chartDate.push(chartData.data?.list)
      // this.chartDate.push(obj)
    }
  }
})

export default useMainStore
